package com.sina.shop.tool;

import com.sina.shop.constant.Constant;

/**
 * 提示信息类
 *
 * @author Administrator
 */


public class ApiResult {
    public ApiBody result(int code, String msg, Object ... data) {
        if (code != Constant.SUCCESS_CODE && code != Constant.ERROR_CODE) {
            throw new IllegalArgumentException("code must be 500 or 200");
        }

        ApiBody apiBody = new ApiBody();
        apiBody.setCode(code);
        apiBody.setMessage(msg);
        //判断data是否为null
        if (data!= null) {
            apiBody.setData(data);
        }

        return apiBody;
    }

    /**
     * 成功返回
     *
     * @param msg
     * @param data 为null时，返回的data值为null
     * @return
     */
    //data为null时，返回的data值为null
    public ApiBody success(String msg, Object ...data) {
        return result(Constant.SUCCESS_CODE, msg, data);
    }

    /**
     * 失败返回
     *
     * @param msg
     * @return
     */
    public ApiBody error(String msg) {
        return result(Constant.ERROR_CODE,msg);
    }

}
